#ifndef DB_DBSERVER_H
#define DB_DBSERVER_H
//------------------------------------------------------------------------------
/**
    @class Db::DbServer
  
    Provides highlevel access to the world database.
    
    (C) 2006 Radon Labs GmbH
*/
#include "core/refcounted.h"
#include "core/singleton.h"
#include "addons/db/database.h"
#include "addons/db/sqlite3/sqlite3factory.h"

//------------------------------------------------------------------------------
namespace Db
{
class DbServer : public Core::RefCounted
{
    DeclareClass(DbServer);
    DeclareSingleton(DbServer);
public:
    /// constructor
    DbServer();
    /// destructor
    virtual ~DbServer();

    /// open the db subsystem in NewGame mode
    bool OpenNewGame(const Util::String& profileURI, const Util::String& dbURI);
    /// open the db subsysten in ContinueGame mode
    bool OpenContinueGame(const Util::String& profileURI);
    /// open the db subsystem in LoadGame mode
    bool OpenLoadGame(const Util::String& profileURI, const Util::String& dbURI, const Util::String& saveGameURI);
    /// delete current game database file
    void DeleteCurrentGame(const Util::String& profileURI);
    /// directly open a world database
    bool Open(const Util::String& dbURI);
    /// close the database subsystem
    void Close();
    /// return true if the database subsystem is open
    bool IsOpen() const;
    /// create a save game
    bool CreateSaveGame(const Util::String& profileURI, const Util::String& dbURI, const Util::String& saveGameURI);
    /// direct access to world database
    const Ptr<Database>& GetDatabase() const;
    /// return true if a current world database exists
    bool CurrentGameExists(const Util::String& profileURI) const;
    /// set accessmode
    void SetAccessMode(Db::Database::AccessMode mode);

private:
    bool isOpen;
    Ptr<Db::Sqlite3Factory> dbFactory;
    Ptr<Database> db;
    Db::Database::AccessMode accessMode;
}; 

//------------------------------------------------------------------------------
/**
*/
inline bool
DbServer::IsOpen() const
{
    return this->isOpen;
}

//------------------------------------------------------------------------------
/**
*/
inline const Ptr<Database>&
DbServer::GetDatabase() const
{
    return this->db;
}

//------------------------------------------------------------------------------
/**
*/
inline void 
DbServer::SetAccessMode(Db::Database::AccessMode mode)
{
    this->accessMode = mode;
}
} // namespace Db
//------------------------------------------------------------------------------
#endif